<?php
namespace common\modules\social;

class ModuleInfo extends \common\core\modularity\ModuleInfo
{
    public function init()
    {
        parent::init();

        $this->id = 'social';
        $this->name = '社交';
        $this->description = '社交模块';
        $this->version = '1.0.0';
        $this->icon = '<i class="fa fa-share-alt"></i>';
        $this->has_backend = true;
        $this->has_frontend = true;
        $this->has_api = false;

        $this->is_system = false;
    }
    
    public function install()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
    
        $this->createTable('{{%friendship}}', [
            'id' => $this->primaryKey(),
            'who_follow' => $this->integer()
            ->notNull(),
            'follow_who' => $this->integer()
            ->notNull(),
            'created_at' => $this->integer()
        ], $tableOptions);
        
        $this->createTable('{{%interest}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer()
            ->notNull(),
            'object_type' => $this->integer()
            ->notNull(),
            'object_id' => $this->integer()
            ->notNull(),
            'created_at' => $this->integer()
        ], $tableOptions);
        
        
        
        $this->createTable('{{%message}}', [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer(),
            'title' => $this->string(1024),
            'content' => $this->text(),
            'type' => $this->smallInteger(4),
            'to_user_id' => $this->integer()
            ->notNull(),
            'from_user_id' => $this->integer()
            ->notNull(),
            'is_read' => $this->smallInteger(1),
            'sort' => $this->integer(),
            'status' => $this->smallInteger(),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);
        
        $this->addForeignKey('fk_friendship_who_follow', '{{%friendship}}', 'who_follow', '{{%user}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_friendship_follow_who', '{{%friendship}}', 'follow_who', '{{%user}}', 'id', 'cascade', 'cascade');
        
        $this->addForeignKey('fk_interest_user', '{{%interest}}', 'user_id', '{{%user}}', 'id', 'cascade', 'cascade');
        
        $this->addForeignKey('fk_message_parent', '{{%message}}', 'parent_id', '{{%message}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_message_to_user', '{{%message}}', 'to_user_id', '{{%user}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_message_from_user', '{{%message}}', 'from_user_id', '{{%user}}', 'id', 'cascade', 'cascade');
    }
    
    public function uninstall()
    {
        $this->dropForeignKey('fk_message_from_user', '{{%message}}');
        $this->dropForeignKey('fk_message_to_user', '{{%message}}');
        $this->dropForeignKey('fk_message_parent', '{{%message}}');
        
        $this->dropForeignKey('fk_interest_user', '{{%interest}}');
        
        $this->dropForeignKey('fk_friendship_follow_who', '{{%friendship}}');
        $this->dropForeignKey('fk_friendship_who_follow', '{{%friendship}}');
        
        $this->dropTable('{{%message}}');
        $this->dropTable('{{%interest}}');
        $this->dropTable('{{%friendship}}');
    }
}